package com.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.beans.Company;
import com.beans.MsgReply;
import com.beans.UCHistory;
import com.beans.User;
import com.service.CompanyManager;
import com.vo.Page;

public class CompanyController extends UniversalController {
	private CompanyManager companyManager;
	
	public CompanyManager getCompanyManager() {
		return companyManager;
	}

	public void setCompanyManager(CompanyManager companyManager) {
		this.companyManager = companyManager;
	}
	public ModelAndView reg(HttpServletRequest request,HttpServletResponse response){
		Map map = new HashMap();
		String msg=null;
		try {
			String code = (String) request.getParameter("code");
			String validateCode = (String)request.getSession().getAttribute("validateCode");
			if(validateCode==null){
				getMessage(response, "验证码过期！");
				return null;
			}
			if (null == code||"".equals(code) || !code.toUpperCase().equals(validateCode.toUpperCase())){ 
				getMessage(response, "验证码错误！");
				return null;
			}
			Object obj = this.getObjectFromJson(request, response, this.obj);
			getUniversalManager().insert(obj);
			map.put("success", true);
			map.put("o", obj);
			msg="注册成功！";
		} catch (Exception e) {
			e.printStackTrace();
			msg="注册失败！";
			map.put("success", false);
		}
		getMessage(response, msg);
		return null;
	}
	public ModelAndView queryList(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		Map param=new HashMap();
		Map result=new HashMap();
		Company c=new Company();
		bind(request,c);
		if(c.getcName()!=null&&!"".equals(c.getcName()))param.put("cName", "%"+c.getcName()+"%");
		Page page=companyManager.getCompanyDao().findPageByCriteria(this.getPageNo(request), pageSize, param,Company.class);
		result.put("o", page);
		return new ModelAndView("company/queryCompanyList",result);
	}
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		String loginName = request.getParameter("loginName");
		String pwd = request.getParameter("pwd");
		String code = (String) request.getParameter("code");
		String validateCode = (String)request.getSession().getAttribute("validateCode");
		if(validateCode==null){
			{
				map.put("loginName", loginName);
				map.put("codeError", "验证码过期！");
				return new ModelAndView("company/loginError", map);
			}
		}
		if (null == code||"".equals(code) || !code.toUpperCase().equals(validateCode.toUpperCase())) 
			{
				map.put("loginName", loginName);
				map.put("codeError", "验证码错误！");
				return new ModelAndView("company/loginError", map);
			}
		Company c = companyManager.getCompanyDao().login(loginName, pwd);
		if (c== null) {
			map.put("loginName", loginName);
			return new ModelAndView("company/loginError", map);
		}
		request.getSession().setAttribute("company", c);
		map.put("o", c);
		return new ModelAndView("Home", map);
	}
	public ModelAndView loginout(HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().removeAttribute("company");
		request.getSession().invalidate();
		return new ModelAndView("company/loginout");
	}
	//修改界面
	public ModelAndView modifyCompany(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Company c=(Company)request.getSession().getAttribute("company");
		result.put("o", c);
		return new ModelAndView("company/modifyCompany", result);
	}
	//更新数据
	public ModelAndView updateCompany(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Company c=(Company)request.getSession().getAttribute("company");
		Company c2=null;
		String msg="";
		try {
			 c2=(Company)this.getObjectFromJson(request, response, this.obj);
			 c.setAddress(c2.getAddress());
			 c.setBuildDate(c2.getBuildDate());
			 c.setcName(c2.getcName());
			 c.setEmail(c2.getEmail());
			 c.setEmployee(c2.getEmployee());
			 c.setFax(c2.getFax());
			 c.setHomePage(c2.getHomePage());
			 c.setKind(c2.getKind());
			 c.setLinkman(c2.getLinkman());
			 c.setMoney(c2.getMoney());
			 c.setPostcode(c2.getPostcode());
			 c.setSummary(c2.getSummary());
			 c.setTel(c2.getTel());
			 companyManager.getCompanyDao().update(c);
			 msg="操作成功！";
		} catch (Exception e) {
			 msg="操作失败！";
			e.printStackTrace();
		}
		getMessage(response, msg);
		return null;
	}
	public ModelAndView queryUserList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map param = new HashMap();
		Map result = new HashMap();
		User user=new User();
		user.setRealName(request.getParameter("realName"));
		user.setSex(request.getParameter("sex"));
		String workYear=request.getParameter("workYear");
		try{
			if(workYear!=null)
			user.setWorkYear(Integer.parseInt(workYear));
		}catch(Exception e){
			e.printStackTrace();
		}
		user.setDegree(request.getParameter("degree"));
		Page page=companyManager.getUserDao().list(this.getPageNo(request), pageSize, user);
		result.put("o", page);
		return new ModelAndView("company/queryUserlist", result);
	}
	public ModelAndView showCompany(HttpServletRequest request,
			HttpServletResponse response) {
		User u=(User)request.getSession().getAttribute("user");
		Map result = new HashMap();
		String id=request.getParameter("id");
		Company c=(Company)getUniversalManager().findByID(Company.class, Integer.parseInt(id));
		result.put("o", c);
		if(u!=null){
			UCHistory uch=new UCHistory();
			uch.setCompanyid(c.getId());
			uch.setUserid(u.getId());
			getUniversalManager().insert(uch);
		}
		return new ModelAndView("company/showCompany", result);
	}
	public ModelAndView getCheckCompanyLoginName(HttpServletRequest request,
			HttpServletResponse response) {
		Map map = new HashMap();
		map.put("loginName", request.getParameter("loginName"));
		Page page=(Page)getUniversalManager().findPageByCriteria(1, 1, map, Company.class);
		if(page.getDataList().size()==0)
			getMessage(response, "恭喜你，可以使用！");
		else
			getMessage(response, "很抱歉，不能使用！");
		return null;
	}
	public ModelAndView companyPwd(HttpServletRequest request,
			HttpServletResponse response) {
		Company c=(Company)request.getSession().getAttribute("company");
		String oldpwd=request.getParameter("oldPwd");
		if(oldpwd==null)return new ModelAndView("company/companyPwd");
		String pwd1=request.getParameter("pwd1");
		if(companyManager.getCompanyDao().companyPwd(c.getId(), oldpwd, pwd1))
			getMessage(response, "操作成功！");
		else
			getMessage(response, "操作失败！");
		return null;
	}
	public ModelAndView companyMsgReplyList(HttpServletRequest request,
			HttpServletResponse response) {
		Map result = new HashMap();
		Map param = new HashMap();
		Company c=(Company)request.getSession().getAttribute("company");
		String pid = request.getParameter("pid");
		try {
			param.put("tableName", "j_company");
			param.put("pid", c.getId());
			Page page = getUniversalManager().findPageByCriteria(
					this.getPageNo(request), pageSize, param, MsgReply.class);
			result.put("o", page);
			result.put("companyID", c.getId());
			return new ModelAndView("company/msgReplyList", result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
